package com.axonactive.yolo.dto;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.axonactive.yolo.domain.TaskNotification;


public class TaskNotificationDTO {

	private int id;
	
	private int userId;
	
	private String taskId;
	
	private String taskName;
	
	private String taskInfo;
	
	private int status;
	
	private int isRead;
	
	private String saveTime;
	
	private String driftTime;
	
	public TaskNotificationDTO(TaskNotification taskNotification) {
		this.id = taskNotification.getId();
		this.userId = taskNotification.getUserId();
		this.taskId = taskNotification.getTaskId();
		this.taskName = taskNotification.getTaskName();
		this.taskInfo = taskNotification.getTaskInfo();
		this.status = taskNotification.getStatus();
		this.isRead = taskNotification.getIsRead();
		this.saveTime = taskNotification.getSaveTime();
		setDriftTime(taskNotification.getSaveTime());
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public String getTaskId() {
		return taskId;
	}

	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}

	public String getTaskName() {
		return taskName;
	}

	public void setTaskName(String taskName) {
		this.taskName = taskName;
	}

	public String getTaskInfo() {
		return taskInfo;
	}

	public void setTaskInfo(String taskInfo) {
		this.taskInfo = taskInfo;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public int getIsRead() {
		return isRead;
	}

	public void setIsRead(int isRead) {
		this.isRead = isRead;
	}

	public String getSaveTime() {
		return saveTime;
	}

	public void setSaveTime(String saveTime) {
		this.saveTime = saveTime;
	}

	public String getDriffTime() {
		return driftTime;
	}

	public void setDriftTime(String inputTime) {
		String returnValue = "";
		
		try {
			Date oldDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(inputTime);
			Date currentDate = new Date();			
			String []theMonths = {"January", "February", "March", "April", "May",
			          				"June", "July", "August", "September", "October", "November", "December"};
			
			double drift = currentDate.getTime() - oldDate.getTime();
			int seconds = (int) (drift / 1000 % 60);
			int minutes = (int) (drift / (60 * 1000) % 60) ;
			int hours = (int) (drift / (60 * 60 * 1000) % 24);
			int days = (int) (drift / (24 * 60 * 60 * 1000));
			int years = currentDate.getYear() - oldDate.getYear();
			int hour = oldDate.getHours();
			int day = oldDate.getDate();
			int month = oldDate.getMonth();
			String year = inputTime.substring(0, 4);
			
			if (seconds < 60 && minutes == 0 
					&& hours == 0 && days == 0 && years == 0) {
						returnValue = "a few seconds ago";
			} 
            else if (minutes == 1 && hours == 0 
                     && days == 0 && years == 0) {
                returnValue = "about a minute ago";                
            }
			else if (minutes < 60 && hours == 0 
                     && days == 0 && years == 0) {
				returnValue = minutes + " minutes ago";
			} 
            else if (hours == 1 && days == 0
                    && years == 0) {
                returnValue = "about an hour ago";
            }
			else if (hours < 24 && days == 0
                    && years == 0) {
				returnValue = hours + " hours ago";
			}
			else if (days == 1 && years == 0) {            
                if (hour >= 0 && hour < 12) {
                    returnValue = "Yesterday at " + inputTime.substring(11,16) + "am";
                }
                else {
                    returnValue = "Yesterday at " + inputTime.substring(11,16) + "pm";
                }
            }
			 else if (days > 1 && years == 0) { 
                if (hour >= 0 && hour < 12) {
                    returnValue = theMonths[month-1] + " " + day + " at " + inputTime.substring(11,16) + "am";
                }
                else {
                    returnValue = theMonths[month-1] + " " + day + " at " + inputTime.substring(11,16) + "pm";
                }
	        }
			 else if (years >= 1) {
                if (hour >= 0 && hour < 12) {
                    returnValue = theMonths[month-1] + " " + day + ", " + year + " at " + inputTime.substring(11,16) + "am";
                }
                else {
                    returnValue = theMonths[month-1] + " " + day + ", " + year + " at " + inputTime.substring(11,16) + "pm";   
                }
	        } 
						
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		this.driftTime = returnValue;
	}
}
